package com.dc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;


@Controller
public class UploadController {
    @RequestMapping(value = "/fileUpload2")
    @ResponseBody
    public String test(MultipartFile file,HttpServletRequest request) throws IOException{
        String path = request.getSession().getServletContext().getRealPath("/images");
        System.out.println("路径："+path);
        String fileName = file.getOriginalFilename();
        System.out.println("文件名称"+fileName);
        File dir = new File(path, fileName);
        System.out.println("判断目录是否存在："+dir.exists());
        if(!dir.exists()){
            dir.mkdirs();
        }
//      MultipartFile自带的解析方法
        file.transferTo(dir);
        //这里我在拼接出我图片要显示的url
        String imageurl = "http://localhost:9527/shoes_Web_exploded/images/"+fileName;
        //将数据库中信息更新
        return "成功";
    }
}
